package cn.zz.veggie.persistence.repository;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.exception.classes.FatalErrorException;
import cn.zz.comm.util.CollectorUtil;
import cn.zz.comm.util.NumberUtil;
import cn.zz.veggie.model.domain.DelMan;
import cn.zz.veggie.model.query.DelManQuery;
import cn.zz.veggie.persistence.dao.DelManMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongq
 * @since 2024/7/14
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class DelManRepository {
	private final DelManMapper delManMapper;

	public DelMan findById(Integer id) {
		if (NumberUtil.isPositive(id)) {
			return delManMapper.selectById(id);
		}
		return null;
	}

	public Map<Integer, DelMan> findByIds(Collection<Integer> ids) {
		if (CollectionUtil.isEmpty(ids)) {
			return Collections.emptyMap();
		}
		final List<DelMan> records = delManMapper.selectBatchIds(ids);
		return CollectorUtil.btmByKeyFun(records, DelMan::getId);
	}

	public DelMan findOne(DelManQuery query) {
		final LambdaQueryWrapper<DelMan> queryWrapper = query.toQueryWrapper();
		final Page<DelMan> page = query.toPage(PageQuery.first().setSearchCount(false));
		return CollUtil.getFirst(delManMapper.selectList(page, queryWrapper));
	}

	public List<DelMan> findByPage(DelManQuery query, PageQuery page) {
		final LambdaQueryWrapper<DelMan> queryWrapper = query.toQueryWrapper();
		final Page<DelMan> adminPage = query.toPage(page);
		final List<DelMan> admins = delManMapper.selectList(adminPage, queryWrapper);
		page.setTotal(((int) adminPage.getTotal()));
		return admins;
	}

	public List<DelMan> findWithPage(DelManQuery query, PageQuery page) {
		final LambdaQueryWrapper<DelMan> queryWrapper = query.toQueryWrapper();
		final Page<DelMan> adminPage = query.toPage(page).setSearchCount(false);
		return delManMapper.selectList(adminPage, queryWrapper);
	}

	public void save(DelMan delMan) {
		Objects.requireNonNull(delMan);
		if (delMan.getId() == null) {
			// insert
			final int effect = delManMapper.insert(delMan);
			if (effect != 1) {
				throw new FatalErrorException("created delMan fail ");
			}
			log.info("created delMan for {}", delMan.getId());
		} else {
			// update
			final int effect = delManMapper.updateById(delMan);
			if (effect != 1) {
				throw new FatalErrorException("update delMan fail ");
			}
			log.info("updated delMan for {}", delMan.getId());
		}
	}

	public void deleteById(Integer id) {
		Objects.requireNonNull(id);
		final int effect = delManMapper.deleteById(id);
		if (effect != 1) {
			log.error("delete");
			throw new FatalErrorException("delete delMan fail ");
		}
		log.info("delete delMan for {}", id);
	}

	public void deleteByIds(Collection<Integer> ids) {
		if (CollUtil.isEmpty(ids)) {
			return;
		}
		final int effect = delManMapper.deleteByIds(ids);
		log.info("delete delMan for {}", effect);
	}

}